package com.iot.platform.mvc.subscription.controller;

import javax.validation.Valid;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.iot.common.core.exception.ThingModelAbilityException;
import com.iot.common.core.util.R;
import com.iot.common.core.util.UuidUtil;
import com.iot.common.security.annotation.SecurityAuthorization;
import com.iot.platform.api.domain.Subscription;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/sub")
@Api(value = "SubscriptionController", tags = "功能")
public class SubscriptionController {

	@SecurityAuthorization
	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<Subscription>> list(
			@RequestParam(name = "productId", required = false, defaultValue = "") String productId,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		String sqlExceptSelect = " FROM " + Subscription.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(productId)) {
			sqlExceptSelect += " AND ( O.productId = '" + productId + "') ";
		}
		Page<Subscription> pages = Subscription.dao.paginate(cur, limit, "select O.* ", sqlExceptSelect);
		return R.ok(pages);
	}

	@SecurityAuthorization
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<Subscription> save(@Valid Subscription ability) {
		if (ability == null) {
			throw new ThingModelAbilityException("参数必填！");
		}
		if (StrKit.isBlank(ability.getId())) {
			ability.setId(UuidUtil.getUUID());
			if (ability.save()) {
				return R.ok();
			}
		} else {
			if (ability.update()) {
				return R.ok();
			}
		}
		return R.failed();
	}

	@SecurityAuthorization
	@RequestMapping(value = "/getById", method = RequestMethod.GET)
	public R<Subscription> getById(
			@RequestParam(name = "SubscriptionId", required = false, defaultValue = "") String SubscriptionId) {
		Subscription subscription = Subscription.dao.findById(SubscriptionId);
		if (subscription == null) {
			throw new ThingModelAbilityException("功能不存在！");
		}
		return R.ok(subscription);
	}

	@SecurityAuthorization
	@PostMapping(value = { "/deleteById" }, produces = "application/json; charset=utf-8")
	public R<Boolean> deleteById(@RequestParam(name = "id", required = true) String id) throws Exception {
		if (Subscription.dao.deleteById(id)) {
			return R.ok(true);
		} else {
			return R.failed(false);
		}
	}

}